package com.action.setting;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.action.common.Ajax;
import com.action.common.WEB;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.basic.DEPT;
import com.pojo.basic.Module;
import com.pojo.basic.ModuleUsers;
import com.pojo.basic.SALM;
import com.pojo.system.PSWD;
import com.service.basic.IDEPTService;
import com.service.basic.IModuleService;
import com.service.basic.IModuleUsersService;
import com.service.basic.ISALMService;
import com.util.ActionLog;
import com.vo.setting.ModuleVo;

public class ModuleAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private IModuleService moduleService;
	private ISALMService salmService;
	private IDEPTService deptService;
	private IModuleUsersService moduleUsersService;
	private List<Module> list;
	private List<PSWD> pswdList;
	private List<SALM> listSalm;
	private ModuleVo  moduleVo;
	private Module  module;
	private List<DEPT> deptList;
	private List<ModuleUsers> moduleUsersList;
	private String p_no;
	//   setter getter  method//
	
	public ModuleVo getModuleVo() {
		return moduleVo;
	}

	public String getP_no() {
		return p_no;
	}

	public void setP_no(String p_no) {
		this.p_no = p_no;
	}

	public void setDeptService(IDEPTService deptService) {
		this.deptService = deptService;
	}

	public void setModuleVo(ModuleVo moduleVo) {
		this.moduleVo = moduleVo;
	}

	public List<Module> getList() {
		return list;
	}

	public void setList(List<Module> list) {
		this.list = list;
	}

	public List<PSWD> getPswdList() {
		return pswdList;
	}

	public void setPswdList(List<PSWD> pswdList) {
		this.pswdList = pswdList;
	}

	public void setModuleService(IModuleService moduleService){
		this.moduleService = moduleService;
	}
	
	public List<SALM> getListSalm() {
		return listSalm;
	}

	public void setListSalm(List<SALM> listSalm) {
		this.listSalm = listSalm;
	}

	public void setSalmService(ISALMService salmService) {
		this.salmService = salmService;
	}
	
	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public List<DEPT> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<DEPT> deptList) {
		this.deptList = deptList;
	}

	public void setModuleUsersService(IModuleUsersService moduleUsersService) {
		this.moduleUsersService = moduleUsersService;
	}

	public List<ModuleUsers> getModuleUsersList() {
		return moduleUsersList;
	}

	public void setModuleUsersList(List<ModuleUsers> moduleUsersList) {
		this.moduleUsersList = moduleUsersList;
	}

	//   action method //
	public String findAllModule(){
		List<Module> temps = moduleService.findAllModule();
		if(temps== null || temps.size()==0){
			return "uninit";
		}
		this.setList(temps);
		return SUCCESS;
	}
	
	public String findAllModuleByRoot(){
		List<Module> temps = moduleService.findAllChildModule("ROOT");
		List<Module> modules = new ArrayList<Module>();
		for(Module temp:temps){
			List<Module> ptemps = moduleService.findAllChildModule(temp.getNo());
			for(Module ptemp : ptemps){
				Module  module = new Module();
				module.setId(ptemp.getId());
				module.setName(temp.getName()+"-->"+ptemp.getName());
				module.setNo(ptemp.getNo());
				module.setP_NO(ptemp.getP_NO());
				module.setUrl(ptemp.getUrl());
				modules.add(module);
			}
		}
		if(modules== null || modules.size()==0){
			return "uninit";
		}
		this.setList(modules);
		return SUCCESS;
	}
	
	public String initModulePermissionData(){
		List<SALM> salms = salmService.findAllSALM();
		this.setListSalm(salms);
		List<DEPT> depts = deptService.findAllDEPT();
		this.setDeptList(depts);
		List<ModuleUsers> moduleusers = moduleUsersService.findModuleUsersByModuleId(this.getModuleVo().getId());
		this.setModuleUsersList(moduleusers);
		Module  temp  = moduleService.get(this.getModuleVo().getId());
		this.setModule(temp);
		return SUCCESS;
	}
	
	public String searchModulePermissionData(){
		String dept  = this.getModuleVo().getDept();
		String keyword = this.getModuleVo().getKeyword();
		try {
			keyword = new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}   
		List<SALM> salms = salmService.searchAllSALM(dept, keyword);
		this.setListSalm(salms);
		List<DEPT> depts = deptService.findAllDEPT();
		this.setDeptList(depts);
		List<ModuleUsers> moduleusers = moduleUsersService.findModuleUsersByModuleId(this.getModuleVo().getId());
		this.setModuleUsersList(moduleusers);
		Module  temp  = moduleService.get(this.getModuleVo().getId());
		this.setModule(temp);
		System.out.println(dept);
		System.out.println(keyword);
		return SUCCESS;
	}
	
	public String saveModulePermissionData(){
		if(this.getModuleVo().getView() != null){
			System.out.println(this.getModuleVo().getId());
			System.out.println(this.getModuleVo().getView().length);
			moduleUsersService.savaOrUpdate(this.getModuleVo().getId(), this.getModuleVo().getView());
		}
		List<SALM> salms = salmService.findAllSALM();
		this.setListSalm(salms);
		List<DEPT> depts = deptService.findAllDEPT();
		this.setDeptList(depts);
		List<ModuleUsers> moduleusers = moduleUsersService.findModuleUsersByModuleId(this.getModuleVo().getId());
		this.setModuleUsersList(moduleusers);
		Module  temp  = moduleService.get(this.getModuleVo().getId());
		this.setModule(temp);
		return SUCCESS;
	}
	
	public void ajaxAllChildModule(){
		ActionLog.info(this.getClass(), "["+WEB.getClassName(this.getClass())+"]:加載樹狀圖子節點：父節點ID："+this.getP_no());
		List<Module> modules = moduleService.findAllChildModule(this.getP_no());
		try {
			Ajax.sendMsgAjax(this.spanListMenu(modules));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String  spanListMenu(List<Module> modules){
		StringBuffer sb = new StringBuffer("[");
		int count = 0;
		for(Module module : modules){
			String leaf = module.getUrl().equals("#")?"false":"true";
			String cls = module.getUrl().equals("#")?"":",'iconCls':'leaf'";
			String href = module.getUrl().equals("#")?"":module.getUrl()+"&moduleId="+module.getId();
			sb.append("{'id':" + "'" +module.getNo() + "'"
					+ ",'text':" + "'" + module.getName() + "'"
					+ ",'leaf':" + ""+leaf+","
					+ "'href':'" + href + "'"+cls+"");
			sb.append("}");
			if (count < modules.size() - 1) {
				sb.append(",");
			}
			count++;
		}
		sb.append("]");
		return sb.toString();
	}
}
